load("@prelude//toolchains/android/tools:build_rules.bzl", "buck_java_binary", "buck_java_library")
load("@prelude//toolchains/android/tools/build_rules:worker.bzl", "remote_worker")
load("@prelude//utils:buckconfig.bzl", "read_list")
load("@prelude//utils:source_listing.bzl", "source_listing")

oncall("android_devxx")

source_listing()

buck_java_library(
    name = "workertool_thrift",
    srcs = [
        "WorkerThriftServer.java",
        "WorkerThriftService.java",
    ],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        "fbcode//remote_execution/lib/if/worker_tool:worker_tool_thrift-java-swift-java17",
        "fbcode//third-party-java/io.airlift:units",
        "fbcode//thrift/facebook/java/swift/swift-codec:swift-codec_lib-java17",
        "fbcode//thrift/lib/java/facebook/service-framework:thrift-metadata-service-handler",
        "fbcode//thrift/lib/thrift:metadata-java-swift-java17",
        "prelude//toolchains/android/src/com/facebook/buck/core/util/log:log",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd:cd",
        "prelude//toolchains/android/src/com/facebook/buck/step:step",
        "prelude//toolchains/android/src/com/facebook/buck/util:process_executor",
        "prelude//toolchains/android/third-party:args4j",
    ],
    exported_deps = [
        "fbsource//third-party/java/org/slf4j:slf4j-simple-1.7.36",
    ],
)

buck_java_library(
    name = "javacd_thrift_worker_tool_lib",
    srcs = [
        "JavaCDThriftWorkerToolMain.java",
    ],
    deps = [
        "fbcode//third-party-java/io.netty:netty-tcnative-boringssl-static",
        "prelude//toolchains/android/src/com/facebook/buck/core/util/log:log",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd:cd",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd/workertool:workertool",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd/workertool/grpc:workertool_grpc",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/java/stepsbuilder/javacd/main:command_lib",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/java/stepsbuilder/javacd/main:main_lib",
        "prelude//toolchains/android/third-party:args4j",
        "prelude//toolchains/android/third-party:guava",
        ":workertool_thrift",
    ],
)

buck_java_library(
    name = "kotlincd_thrift_worker_tool_lib",
    srcs = [
        "KotlinCDThriftWorkerToolMain.java",
    ],
    deps = [
        "fbcode//third-party-java/io.netty:netty-tcnative-boringssl-static",
        "prelude//toolchains/android/src/com/facebook/buck/core/util/log:log",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd:cd",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd/workertool:workertool",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/cd/workertool/grpc:workertool_grpc",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/java/stepsbuilder/javacd/main:command_lib",
        "prelude//toolchains/android/src/com/facebook/buck/jvm/kotlin/cd/workertool:command_lib",
        "prelude//toolchains/android/third-party:args4j",
        "prelude//toolchains/android/third-party:guava",
        ":workertool_thrift",
    ],
)

buck_java_binary(
    name = "javacd_thrift_worker_tool",
    blocklist = [
        "^META-INF/.*\\.(SF|RSA|DSA|EC)$",
    ],
    main_class = "com.facebook.buck.jvm.remote_worker.JavaCDThriftWorkerToolMain",
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":javacd_thrift_worker_tool_lib",
    ],
)

buck_java_binary(
    name = "kotlincd_thrift_worker_tool",
    blocklist = [
        "^META-INF/.*\\.(SF|RSA|DSA|EC)$",
    ],
    main_class = "com.facebook.buck.jvm.remote_worker.KotlinCDThriftWorkerToolMain",
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":kotlincd_thrift_worker_tool_lib",
    ],
)

remote_worker(
    name = "javacd_remote_worker",
    class_loader_bootstrapper = "prelude//toolchains/android/src/com/facebook/buck/cli/bootstrapper:bootstrapper",
    exe = ":javacd_thrift_worker_tool",
    jvm_args = read_list(
        "javacd",
        "jvm_args",
        default = [],
        delimiter = " ",
        required = False,
    ),
    main_class = "com.facebook.buck.jvm.remote_worker.JavaCDThriftWorkerToolMain",
    visibility = [
        "PUBLIC",
    ],
)

remote_worker(
    name = "kotlincd_remote_worker",
    class_loader_bootstrapper = "prelude//toolchains/android/src/com/facebook/buck/cli/bootstrapper:bootstrapper",
    exe = ":kotlincd_thrift_worker_tool",
    jvm_args = read_list(
        "kotlincd",
        "jvm_args",
        default = [],
        delimiter = " ",
        required = False,
    ),
    main_class = "com.facebook.buck.jvm.remote_worker.KotlinCDThriftWorkerToolMain",
    visibility = [
        "PUBLIC",
    ],
)
